<!DOCTYPE html>
<html>

<head>
    <title>编辑游记日报</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/js/plugins/choosen/component-chosen.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/choosen/chosen.jquery.js"></script>
    <script src="/js/common.js"></script>
    <script>
        $(function () {


            var Edr = new Eleditor({
                el: '#contentEditor',
                upload:{
                    server: '/images',
                },
            });

            $('#changeBtn').click(function () {
                $('#imageFile').click();
            });
            $('#imageFile').change(function () {
                $('#coverForm').ajaxSubmit(function (data) {
                    if (data.status == 1) {
                        $('#coverUrl').prop('src', data.url);
                    }
                });
            });
            //数据回显操作---------------------------------------------------
            // 获取请求中的参数
            var params = getParams();
            var id = params.id;
            if(id){
                // 根据id查询数据进行回显
                $.get('/dailys/'+ id,function (data) {
                    console.log(data);
                    $('#daliy').renderValues(data);

                });
                // 如果回显数据,那么是编辑操作,发送的是PUT请求
                $("#detailForm input[name='_method']").val("PUT");
            }



            //保存或更新操作-----------------------------------------------------------
            $('.submitBtn').click(function () {
                var status = $(this).data('status');
                $('#detailForm').ajaxSubmit({
                    // 表单提交前执行该方法
                    beforeSubmit: function (params, form, options) {
                        params.push({name: "id", value: id}); // 状态
                        params.push({name: "status", value: status}); // 状态
                        params.push({name: "title", value: $('#title').val()}); // 标题
                        params.push({name: "coverUrl", value: $('#coverUrl').attr('src')}); // 封面
                        params.push({name: "content.content", value: Edr.getContent()}); // 游记内容
                    },
                    success: function (data) {
                        if (data.success) {
                            window.location.href = '/mine/dailys.html';
                        }
                    }
                });
            });

        });
    </script>
</head>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw submitBtn" id="save" data-status="0"><span>保存</span></i>
            <i class="fa fa-check submitBtn" id="release" data-status="1"><span>发布</span></i>
        </div>
    </div>
</div>
<div class="daliys form-group">
    <div id="daliy">
        <div class="cover">
            <img id="coverUrl" width="100%" render-src="coverUrl" height="200px"/>
            <form id="coverForm" action="/images" method="post" enctype="multipart/form-data">
                <input type="file" name="file" style="display: none" accept="image/gif,image/jpeg,image/jpg,image/png"
                       id="imageFile"/>
                <button type="button" class="btn" id="changeBtn">
                    <i class="fa fa-pencil-square-o"></i>
                    <small>更换封面</small>
                </button>
            </form>
        </div>


        <form id="detailForm" action="/dailys" method="post">
            <input type="hidden" name="_method"/>
            <div class="title">
                <input class="form-control" id="title" render-value="title" name="title" placeholder="请输入标题"/>
            </div>
            <h6 href="#">点击文章内容即可编辑</h6>
            <div id="contentEditor" render-html="content.content">
            </div>
            <hr/>
        </form>
    </div>
</div>


</body>

</html>